#include "RadianMainRenderWnd.h"
#include "RadianMainEditorController.h"
#include "OgreMesh.h"
#include "OgreMeshManager.h"
#include "OgreMaterialManager.h"
#include "OgreEntity.h"
#include "OgrePlane.h"
#include "OgreResourceGroupManager.h"
#include "OgreAnimationState.h"
#include "OgreRenderWindow.h"
#include "OgreSceneManager.h"

//--------------------------------------------------------------------------
RadianMainRenderWnd::RadianMainRenderWnd(QWidget* parentWidget)
	:RadianRenderWnd("RadianMainRenderWindow",parentWidget,true)
	,m_RadianEditorController(NULL)
{
	
}
//--------------------------------------------------------------------------
RadianMainRenderWnd::~RadianMainRenderWnd()
{
	
}
//--------------------------------------------------------------------------
void RadianMainRenderWnd::Update(float timeElapsed, bool realTime)
{
	
	std::vector<Ogre::Entity*>::iterator testEntityIt = mTestMeshes.begin();
	while( testEntityIt != mTestMeshes.end())
	{
		Ogre::ConstEnabledAnimationStateIterator enableAnmStateIt = (*testEntityIt)->getAllAnimationStates()->getEnabledAnimationStateIterator();
		while(enableAnmStateIt.hasMoreElements())
		{
			(*enableAnmStateIt.current())->addTime(0.033f);
			(*testEntityIt)->getSkeleton()->_notifyManualBonesDirty();
			enableAnmStateIt.moveNext();
		}

		testEntityIt++;
	}

	RadianRenderWnd::Update(timeElapsed,realTime);
	m_RadianEditorController->Update(timeElapsed);
}
//--------------------------------------------------------------------------
bool RadianMainRenderWnd::Initialize()
{
	if(RadianRenderWnd::Initialize())
	{
		m_RadianEditorController = new RadianMainEditorController(mCamera, mParentWidget,this);
		return true;
	}

	return false;	
}
//--------------------------------------------------------------------------
void RadianMainRenderWnd::SceneSetup()
{
	if(!Ogre::MeshManager::getSingletonPtr()->resourceExists(G_EditorBaseRefPlaneName))
	{
		Ogre::Plane plane(Math::Vector3::UNIT_Y,0);

		Ogre::MeshPtr refPlaneMesh
			= Ogre::MeshManager::getSingletonPtr()->createPlane( G_EditorBaseRefPlaneName
			, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
			, plane
			, 3000, 3000, 200, 200
			, true
			, 1, 100, 100
			, Math::Vector3::UNIT_Z);
	}

	Ogre::Entity* ogreHead = mSceneMgr->createEntity("ogrehead.mesh");

	Ogre::Entity* refPlaneEntity = mSceneMgr->createEntity(mRenderWndName + "_RefEntity", G_EditorBaseRefPlaneName);
	refPlaneEntity->setMaterialName("Ocean2_HLSL_GLSL");
	Ogre::SceneNode* refPlaneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); 
	refPlaneNode->attachObject(refPlaneEntity);
	mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ogreHead);
	refPlaneNode->setPosition(Math::Vector3::ZERO);
	ogreHead->getParentSceneNode()->setPosition(Math::Vector3::ZERO);
	mSceneMgr->setSkyBox(true,"Examples/MorningSkyBox");

	for(int i = 0;i < 30; i++)
	{
		Ogre::Entity* testEntity = mSceneMgr->createEntity("jaiqua.mesh");
		Ogre::SceneNode* testNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

		testNode->setPosition(Ogre::Math::RangeRandom(-500,500),0,Ogre::Math::RangeRandom(-500,500));
		testNode->yaw(Math::Radian(Ogre::Math::AngleUnitsToRadians(Ogre::Math::RangeRandom(0,360))));
		testNode->scale(Math::Vector3(3, 3, 3));
		testNode->attachObject(testEntity);

		Ogre::AnimationState* anmationState = NULL;

		if( i < 5 )
			anmationState = testEntity->getAllAnimationStates()->getAnimationState("Run");
		else if( i >= 5 && i < 15 )
			anmationState = testEntity->getAllAnimationStates()->getAnimationState("Sneak");

		else if( i >= 15 && i <20 )
			anmationState = testEntity->getAllAnimationStates()->getAnimationState("Stagger");

		else if( i >= 20 && i < 25 )
			anmationState = testEntity->getAllAnimationStates()->getAnimationState("Turn");

		else if( i >= 25 && i < 30 )
			anmationState = testEntity->getAllAnimationStates()->getAnimationState("Walk");


		anmationState->setEnabled(true);
		anmationState->setLoop(true);
		anmationState->setTimePosition(i);
		mTestMeshes.push_back(testEntity);
	}
}
//--------------------------------------------------------------------------

